<template>
<div class="">
    <div class="float_left tree_box">
        <h4 class="margin_bottom20">商品分类</h4>
        <el-button type="primary" size="small" @click="dialogFormVisible = true"><i class="iconfont icon-plus"></i>新增</el-button>
        <el-button type="danger" size="small"  icon="delete" @click="deleteItemCat(selectedItemCat.id)" :disabled="!selected">删除</el-button>
        <div class="clear"></div>
        <br>
        <el-tree
            :data="treeData"
            node-key="id"
            @current-change="currentChange"
            highlight-current
            :load="loadNode"
            lazy>
        </el-tree>
    </div>

    <div class="operate_main">
        <h4 class="margin_bottom20">详细信息</h4>
        <el-form :inline="true" :model="selectedItemCat" label-width="200px">
            <el-form-item label="分类名称：">
                <el-input v-model="selectedItemCat.name"></el-input>
            </el-form-item>
            <el-form-item label="是否为父节点：">
                <el-radio-group v-model="selectedItemCat.isParent">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">否</el-radio>
                    </el-radio-group>
            </el-form-item>
            <el-form-item label="选择父节点：">
                <el-input :disabled="!selected" @focus="loadParentData2" v-model="parentName"></el-input>
            </el-form-item>
            <el-form-item label="排序优先值：">
                <el-input v-model="selectedItemCat.sortOrder" placeholder="值越小排序越前"></el-input>
            </el-form-item>
            <el-form-item label="是否启用：">
                <el-switch v-model="selectedItemCat.status"></el-switch>
            </el-form-item>
            <el-form-item label="备注：">
                <el-input type="textarea" v-model="selectedItemCat.remark" placeholder="说点什么...最多输入100个字符"></el-input>
            </el-form-item>
            <el-form-item label=" " style="vertical-align: bottom">
                    <el-button type="primary" @click="updateItemCat" :disabled="!selected">保存修改</el-button>
            </el-form-item>
        </el-form>
    </div>

    <el-dialog
        title="选择父节点分类"
        v-model="dialogVisible1">
        <el-tree
            :data="parentData"
            highlight-current
            @node-click="handleNodeClick1"
            default-expand-all>
        </el-tree>
    </el-dialog>

    <el-dialog
        title="选择父节点分类"
        v-model="dialogVisible2">
        <el-tree
            :data="parentData"
            highlight-current
            @node-click="handleNodeClick2"
            default-expand-all>
        </el-tree>
    </el-dialog>

    <el-dialog
        title="新增分类"
        v-model="dialogFormVisible"
        :before-close="handleClose">
        <el-form :model="itemCat">
            <el-form-item label="分类名称：" :rules="[{ required: true, message: '名称不能为空'}]">
                <el-input v-model="itemCat.name"></el-input>
            </el-form-item>
            <el-form-item label="是否为父节点：" :rules="[{ required: true}]">
                <el-radio-group v-model="itemCat.isParent">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">否</el-radio>
                    </el-radio-group>
            </el-form-item>
            <el-form-item label="选择父节点：">
                <el-input @focus="loadParentData1" v-model="label"></el-input>
            </el-form-item>
            <el-form-item label="排序优先值：" :rules="[{ required: true, message: '顺序不能为空'}]">
                <el-input v-model="itemCat.sortOrder" placeholder="值越小排序越前"></el-input>
            </el-form-item>
            <el-form-item label="是否启用：" :rules="[{ required: true}]">
                <el-switch v-model="itemCat.status"></el-switch>
            </el-form-item>
            <el-form-item label="备注：">
                <el-input type="textarea" v-model="itemCat.remark" placeholder="说点什么...最多输入100个字符"></el-input>
            </el-form-item>
            <el-form-item label=" " style="vertical-align: bottom">
                    <el-button type="primary" @click="addItemCat">添加分类</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <div class="clear"></div>
</div>
</template>

<script>
export default {
  data() {
    return {
      treeData: [],
      parentData: [],
      label: "",
      selectedItemCat: {},
      itemCat: {},
      id: 0,
      selected: false,
      dialogFormVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      parentName: ""
    };
  },
  methods: {
        loadNode(node, resolve) {
            this.$axios.get(API.rootPath + "/item-cats/getJsonTree/" + this.id).then((response) => {
                if (response.data.status == 0) {
                    resolve(response.data.data);
                }
            })
        },
        loadParentData1() {
            this.dialogVisible1 = true;
            this.$axios.get(API.rootPath + "/item-cats/getParentTree").then((response) => {
                this.parentData = response.data.data;
            })
        },
        loadParentData2() {
            this.dialogVisible2 = true;
            this.$axios.get(API.rootPath + "/item-cats/getParentTree").then((response) => {
                this.parentData = response.data.data;
            })
        },
        currentChange(data, node) {
            this.selected = true;
            this.id = data.id;
            this.$axios.get(API.rootPath + "/item-cats/" + this.id).then((response) => {
                this.selectedItemCat = response.data.data;
                this.selectedItemCat.status = this.selectedItemCat.status ? true : false;
                this.getParentName();
            });
        },
        getParentName() {
            this.$axios.get(API.rootPath + "/item-cats/" + this.selectedItemCat.parentId).then((response) => {
                if (response.data.hasOwnProperty('data')) {
                    this.parentName = response.data.data.name;
                } else {
                    this.parentName = "";
                }
            })
        },
        handleNodeClick1(val) {
            this.itemCat.parentId = val.id;
            this.label = val.label;
            this.dialogVisible1 = false;
        },
        handleNodeClick2(val) {
            this.parentName = val.label;
            this.selectedItemCat.parentId = val.id;
            this.dialogVisible2 = false;
        },
        addItemCat() {
            this.itemCat.status = this.itemCat.status ? 1 : 0;
            this.$axios.post(API.rootPath + "/item-cats", this.itemCat).then((response) => {
                this.itemCat = {};
                if (response.data.status == 0) {
                    this.$message({
                        message: "添加成功",
                        type: "success",
                    })
                    this.dialogFormVisible = false;
                }
            })
        },
        deleteItemCat(id) {
            this.$axios.delete(API.rootPath + "/item-cats/" + id).then((response) => {
                if (response.data.status == 0) {
                    this.$message({
                        message: "删除成功",
                        type: "success",
                    })
                }
            })
        },
        updateItemCat() {
            this.selectedItemCat.status = this.selectedItemCat.status ? 1 : 0;
            this.$axios.post(API.rootPath + "/item-cats", this.selectedItemCat).then((response) => {
                if (response.data.status == 0) {
                    this.$message({
                        message: "修改成功",
                        type: "success",
                    })
                }
                this.selectedItemCat.status = this.selectedItemCat.status ? true : false;
            })
        },
        handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            this.itemCat = {};
            this.label = "";
            done();
          })
          .catch(_ => {});
    }
  },
  created() {
    // this.$axios.get(API.rootPath + "/item-cats/getJsonTree").then((response) => {
    //   this.treeData = response.data.data;
    // });
  }
};
</script>